package com.egao.common.module.student.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.egao.common.component.elasticsearch.service.ElasticSearchService;
import com.egao.common.core.exception.ParameterException;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.module.push.constants.PushConstants;
import com.egao.common.module.push.mapper.PushMsgMapper;
import com.egao.common.module.student.mapper.StudentMatchingMapper;
import com.egao.common.module.student.service.StudentMatchingService;
import com.egao.common.module.push.entity.PushMsg;
import com.egao.common.module.student.entity.Student;
import com.egao.common.module.student.service.StudentService;
import com.egao.common.module.system.service.YearService;
import com.egao.common.publicly.entity.PubliclyCareerVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 推送信息接口实现类
 *
 * @author pyx
 */
@Service
public class StudentMatchingServiceImpl extends ServiceImpl<StudentMatchingMapper, PushMsg> implements StudentMatchingService {
    @Autowired
    private StudentService studentService;
    @Autowired
    private PushMsgMapper pushMsgMapper;
    @Autowired
    private ElasticSearchService elasticSearchService;

    @Autowired
    private YearService yearService;

    private static String tablePrefix = "push_msg_";

    @Override
    public List<PubliclyCareerVO> listPage(PageParam<PubliclyCareerVO> pageParam, Integer userId) {

        Student student = studentService.getByUserId(userId);

        if (student == null) {
            throw new ParameterException("学生信息不存在");
        }

        String xh = student.getXh();
        if (StrUtil.isBlank(xh)) {
            throw new ParameterException("学号不存在");
        }

        if ("1".equals(pageParam.getPageData().get("type"))) {
            return baseMapper.listRecruit(pageParam, xh);
        } else if ("2".equals(pageParam.getPageData().get("type"))) {
            return baseMapper.listPreach(pageParam, xh);
        } else {
            return baseMapper.listPreachCloud(pageParam, xh);
        }
    }

    @Override
    public List<PubliclyCareerVO> listPageAll(PageParam<PubliclyCareerVO> pageParam, Integer userId) {
        Student student = studentService.getByUserId(userId);

        if (student == null) {
            throw new ParameterException("学生信息不存在");
        }

        String xh = student.getXh();
        if (StrUtil.isBlank(xh)) {
            throw new ParameterException("学号不存在");
        }

        if (student.getYearId() == null) {
            return new ArrayList<>();
        }

        String tableName = tablePrefix + student.getYearId();

        if ("1".equals(pageParam.getPageData().get("type"))) {
            return baseMapper.dynamicListRecruit(pageParam, xh, tableName);
        } else if ("2".equals(pageParam.getPageData().get("type"))) {
            return baseMapper.dynamicListPreach(pageParam, xh, tableName);
        } else {
            return baseMapper.dynamicListPreachCloud(pageParam, xh, tableName);
        }
    }

    @Override
    public JsonResult pushRead(Integer pushMsgId) {
        if(pushMsgId == null){
            return JsonResult.error("没有查询到推送信息");
        }
        PushMsg pushMsg = baseMapper.selectById(pushMsgId);

        if(PushConstants.NO.equals(pushMsg.getReadFlag())){
            pushMsg.setReadFlag(PushConstants.YES);
            pushMsgMapper.updateById(pushMsg);
            List<PushMsg> pushMsgList = new ArrayList<>();
            pushMsgList.add(pushMsg);
            elasticSearchService.savePushList(pushMsgList,pushMsg.getBusinessId(),pushMsg.getType());
        }

        return JsonResult.ok();
    }

    @Override
    public JsonResult pushReadByYear(Integer pushMsgId, Integer year) {
        if(pushMsgId == null){
            return JsonResult.error("没有查询到推送信息");
        }
        String tableName = tablePrefix +year;
        PushMsg pushMsg = baseMapper.dynamicSelectById(pushMsgId, tableName);

        if(PushConstants.NO.equals(pushMsg.getReadFlag())){
            pushMsg.setReadFlag(PushConstants.YES);
            pushMsgMapper.dynamicUpdate(tableName, pushMsg);
        }

        return JsonResult.ok();
    }
}
